package com.zhonghui.dolo.manager.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhonghui.dolo.manager.dao.GoodsCategoryDao;
import com.zhonghui.dolo.manager.model.GoodsCategory;
import com.zhonghui.dolo.manager.model.vo.*;
import com.zhonghui.dolo.manager.service.GoodsCategoryService;
import com.zhonghui.dolo.manager.util.Constans;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryDao goodsCategoryDao;

    @Override
    public GoodsCategoryVo getCategoryVoById(Integer categoryId) {
        GoodsCategory goodsCategory = goodsCategoryDao.getCategoryById(categoryId);
        GoodsCategoryVo goodsCategoryVo = new GoodsCategoryVo();
        if (goodsCategory.getParentId() != null && goodsCategory.getParentId() > 0) {
            // 说明当前节点为二级节点
            GoodsCategory parentGoodsCategory = goodsCategoryDao.getCategoryById(goodsCategory.getParentId());
            if (parentGoodsCategory != null) {
                goodsCategoryVo.setFirstCategory(parentGoodsCategory.getCategoryName());
                goodsCategoryVo.setFirstId(parentGoodsCategory.getCategoryId());
            }
            goodsCategoryVo.setSecondCategory(goodsCategory.getCategoryName());
            goodsCategoryVo.setSecondId(goodsCategory.getCategoryId());
        } else {
            goodsCategoryVo.setFirstCategory(goodsCategory.getCategoryName());
            goodsCategoryVo.setFirstId(goodsCategory.getCategoryId());
        }
        return goodsCategoryVo;
    }

    @Override
    public List<SimpleGoodsCategoryVo> findCategoryListByParentId(Integer parentId) {
        List<GoodsCategory> goodsCategoryList = goodsCategoryDao.findCategoryListByParentId(parentId);
        List<SimpleGoodsCategoryVo> simpleList = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            SimpleGoodsCategoryVo simpleGoodsCategoryVo = new SimpleGoodsCategoryVo();
            BeanUtils.copyProperties(goodsCategory, simpleGoodsCategoryVo);

            simpleList.add(simpleGoodsCategoryVo);
        }
        return simpleList;
    }

    @Override
    public PageResultVo<List<GoodsCategoryDetailVo>> findCategoryList(int pageNum, int pageSize, GoodsCategoryReqVo goodsCategoryReqVo) {
        Page<List<GoodsCategoryDetailVo>> page = PageHelper.startPage(pageNum, pageSize, true);
        List<GoodsCategory> goodsCategoryList = goodsCategoryDao.findCategoryList(goodsCategoryReqVo.getCategoryName());
        List<GoodsCategoryDetailVo> goodsCategoryDetailVos = new ArrayList<>();

        for (GoodsCategory goodsCategory : goodsCategoryList) {
            GoodsCategoryDetailVo goodsCategoryDetailVo = new GoodsCategoryDetailVo();
            BeanUtils.copyProperties(goodsCategory, goodsCategoryDetailVo);
            goodsCategoryDetailVos.add(goodsCategoryDetailVo);
        }
        PageResultVo<List<GoodsCategoryDetailVo>> pageResultVo = new PageResultVo<>();
        pageResultVo.setRes(Constans.RES_SUCCESS);
        pageResultVo.setObj(goodsCategoryDetailVos);
        pageResultVo.setTotal(page.getTotal());
        pageResultVo.setPageSize(pageSize);
        return pageResultVo;
    }

    @Override
    public int add(GoodsCategoryEditVo goodsCategoryEditVo) {
        GoodsCategory category = new GoodsCategory();
        BeanUtils.copyProperties(goodsCategoryEditVo, category);
        Date now = new Date();
        category.setCreateTime(now);
        category.setUpdateTime(now);
        return goodsCategoryDao.add(category);
    }
}
